﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoCadConvert
{
    /// <summary>
    /// TaskItem.xaml 的交互逻辑
    /// </summary>
    public partial class TaskItem : UserControl
    {
        public enum TaskStatus
        {
            WaitForPull=0,
            WaitForFinish=10,
            Finish=40,
        }        
        public delegate void TaskPull(TaskItem item);
        public event TaskPull pullTask;

        //任务id
        public int m_TaskId;
        public int m_PlanId;
        public string m_FileUrl;
        public string m_Preview;
        public string m_PlanInfo;
        public TaskStatus m_Status;
        public TaskItem(BitmapImage image, TaskStatus status,string title,string content,string preview, string date,int taskid,int planid)
        {
            InitializeComponent();
            this.image.Source = image;
            this.title.Text = title;
            this.content.Text = content;
            this.date.Text = date;
            m_Status = status;
            m_Preview = preview;
            m_TaskId = taskid;
            m_PlanId = planid;

            upLoad.Click += UpLoad_Click;
            getTask.Click += GetTask_Click;

            //按钮初始化
            if (m_Status == TaskStatus.WaitForFinish)
                getTask.Content = "开始绘制";
            else if(m_Status == TaskStatus.Finish)
                getTask.Visibility = Visibility.Hidden;
        }
        private void GetTask_Click(object sender, RoutedEventArgs e)
        {
            string btnCotent = getTask.Content as string;
            if (m_Status==TaskStatus.WaitForPull)
            {
                string response = HttpManager.TaskProcess(new int[] { m_PlanId }, new int[] { 10 });
                if (pullTask != null)
                    pullTask(this);
                if(!response.Contains("成功"))
                {
                    MessageBox.Show("服务器连接异常");
                }
            }
            else if(m_Status == TaskStatus.WaitForFinish)
            {
                getTask.Content = "继续绘制";
                upLoad.Visibility = Visibility.Visible;
            }
        }
        private void UpLoad_Click(object sender, RoutedEventArgs e)
        {
            string defaultFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(),"OutPutBsp.xml");
            if (DwgConvert.ExportFile(defaultFilePath))
            {
                Task task = Task.Factory.StartNew(new Action(() => {
                    try
                    {
                        string fileName = "autocad_" + m_TaskId + "_" + DateTime.Now.Ticks + ".xml";
                        string output = HttpManager.Upload(fileName, defaultFilePath);
                        if (!string.IsNullOrEmpty(output))
                        {
                            string response = HttpManager.TaskOutput(m_PlanId, output, m_Preview);
                            response = HttpManager.TaskProcess(new int[] { m_PlanId }, new int[] { 40 });
                            this.Dispatcher.Invoke(new Action(() =>
                            {
                                if (pullTask != null)
                                {//状态修改后移除
                                    pullTask(this);
                                }
                            }));
                        }
                    }
                    catch
                    {
                        MessageBox.Show("服务器连接异常");
                    }
                }));                
            }
        }
        private void test_Click_Reset(object sender, RoutedEventArgs e)
        {
            //for test
            string response = HttpManager.TaskProcess(new int[] { m_PlanId }, new int[] { 0 });
            pullTask(this);
        }
    }
}
